package net.trevize.datasetexplorer;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.Properties;

public class DatasetExplorerProperties {

	private static Properties properties;
	private static final String PROPERTIES_FILEPATH = "./config/DatasetExplorer.properties";
	private static final String PROPERTIES_COMMENTS = "This is the properties file of DatasetExplorer, a Java application for exploring and searching in large collections of annotated images\n#Nicolas James <nicolas.james@gmail.com> [[http://njames.trevize.net]]";

	public static final String PROPERTY_USER_HTTP_URI_PREFIXES = "PROPERTY_USER_HTTP_URI_PREFIXES";
	private static final String USER_HTTP_URI_PREFIXES_PROPERTY_IFS = ",";
	public static final String PROPERTY_USER_FILE_URI_PREFIXES = "PROPERTY_USER_FILE_URI_PREFIXES";
	private static final String USER_FILE_URI_PREFIXES_PROPERTY_IFS = ",";
	public static final String PROPERTY_PASCALVOC_ANNOTATIONS_SUBDIR = "PROPERTY_PASCALVOC_ANNOTATIONS_SUBDIR";
	public static final String PROPERTY_PASCALVOC_JPGIMAGES_SUBDIR = "PROPERTY_PASCALVOC_JPGIMAGES_SUBDIR";
	public static final String PROPERTY_PASCALVOC_SERIALIZED_V_URI_JOBJ_FILENAME = "PROPERTY_PASCALVOC_SERIALIZED_V_URI_JOBJ_FILENAME";
	public static final String PROPERTY_PASCALVOC_SERIALIZED_V_OBJECT_JOBJ_FILENAME = "PROPERTY_PASCALVOC_SERIALIZED_V_OBJECT_JOBJ_FILENAME";
	public static final String PROPERTY_HTML_PATH_ABOUT = "PROPERTY_HTML_PATH_ABOUT";
	public static final String PROPERTY_CSS_PATH_MAIN = "PROPERTY_CSS_PATH_MAIN";
	public static final String PROPERTY_DOCKING_WINDOWS_THEME = "PROPERTY_DOCKING_WINDOWS_THEME";
	public static final String PROPERTY_ICON_PATH_DATASETEXPLORER = "PROPERTY_ICON_PATH_DATASETEXPLORER";

	private static String pascalvoc_annotation_subdir;
	private static String pascalvoc_jpgimages_subdir;
	private static String pascalvoc_serialized_v_uri_jobj_filename;
	private static String pascalvoc_serialized_v_object_jobj_filename;
	private static String[] user_http_uri_prefixes;
	private static String[] user_file_uri_prefixes;
	private static String html_path_about;
	private static String css_path_main;
	private static String docking_windows_theme;
	private static String icon_path_datasetexplorer;

	private static void init() {
		properties = new Properties();
		try {
			properties.load(new FileInputStream(PROPERTIES_FILEPATH));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		String user_http_uri_prefixes_property = properties
				.getProperty(PROPERTY_USER_HTTP_URI_PREFIXES);

		if (user_http_uri_prefixes_property != null) {
			user_http_uri_prefixes = properties.getProperty(
					PROPERTY_USER_HTTP_URI_PREFIXES).split(",");
		} else {
			user_http_uri_prefixes = new String[0];
		}

		String user_local_path_prefixes_property = properties
				.getProperty(PROPERTY_USER_FILE_URI_PREFIXES);

		if (user_local_path_prefixes_property != null) {
			user_file_uri_prefixes = properties.getProperty(
					PROPERTY_USER_FILE_URI_PREFIXES).split(",");
		} else {
			user_file_uri_prefixes = new String[0];
		}

		pascalvoc_annotation_subdir = properties
				.getProperty(PROPERTY_PASCALVOC_ANNOTATIONS_SUBDIR);

		pascalvoc_jpgimages_subdir = properties
				.getProperty(PROPERTY_PASCALVOC_JPGIMAGES_SUBDIR);

		pascalvoc_serialized_v_uri_jobj_filename = properties
				.getProperty(PROPERTY_PASCALVOC_SERIALIZED_V_URI_JOBJ_FILENAME);

		pascalvoc_serialized_v_object_jobj_filename = properties
				.getProperty(PROPERTY_PASCALVOC_SERIALIZED_V_OBJECT_JOBJ_FILENAME);

		html_path_about = properties.getProperty(PROPERTY_HTML_PATH_ABOUT);

		css_path_main = properties.getProperty(PROPERTY_CSS_PATH_MAIN);

		docking_windows_theme = properties
				.getProperty(PROPERTY_DOCKING_WINDOWS_THEME);

		icon_path_datasetexplorer = properties
				.getProperty(PROPERTY_ICON_PATH_DATASETEXPLORER);
	}

	public static String[] getUserHTTPURIPrefixes() {
		if (properties == null) {
			init();
		}

		return user_http_uri_prefixes;
	}

	public static void addUserHTTPURIPrefixes(String new_user_http_uri_prefix) {
		if (properties == null) {
			init();
		}

		String user_http_uri_prefixes_property = properties
				.getProperty(PROPERTY_USER_HTTP_URI_PREFIXES);

		if (user_http_uri_prefixes_property == null) {
			user_http_uri_prefixes_property = new_user_http_uri_prefix;
		} else {

			// first we test if this new prefix is ever stored in the properties
			// file.
			if (Arrays.asList(user_http_uri_prefixes).contains(
					new_user_http_uri_prefix)) {
				return;
			}

			user_http_uri_prefixes_property = user_http_uri_prefixes_property
					+ USER_HTTP_URI_PREFIXES_PROPERTY_IFS
					+ new_user_http_uri_prefix;
		}

		// set the new property.
		properties.setProperty(PROPERTY_USER_HTTP_URI_PREFIXES,
				user_http_uri_prefixes_property);

		// save the properties file.
		try {
			properties.store(new FileOutputStream(PROPERTIES_FILEPATH),
					PROPERTIES_COMMENTS);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		init();
	}

	public static String[] getUserFILEURIPrefixes() {
		if (properties == null) {
			init();
		}

		return user_file_uri_prefixes;
	}

	public static void addUserFILEURIPrefixes(String new_user_file_uri_prefix) {
		if (properties == null) {
			init();
		}

		String user_file_uri_prefixes_property = properties
				.getProperty(PROPERTY_USER_FILE_URI_PREFIXES);

		if (user_file_uri_prefixes_property == null) {
			user_file_uri_prefixes_property = new_user_file_uri_prefix;
		} else {

			// first we test if this new prefix is ever stored in the properties
			// file.
			if (Arrays.asList(user_file_uri_prefixes).contains(
					new_user_file_uri_prefix)) {
				return;
			}

			user_file_uri_prefixes_property = user_file_uri_prefixes_property
					+ USER_FILE_URI_PREFIXES_PROPERTY_IFS
					+ new_user_file_uri_prefix;
		}

		// set the new property.
		properties.setProperty(PROPERTY_USER_FILE_URI_PREFIXES,
				user_file_uri_prefixes_property);

		// save the properties file.
		try {
			properties.store(new FileOutputStream(PROPERTIES_FILEPATH),
					PROPERTIES_COMMENTS);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		init();
	}

	public static String getPascalvoc_annotations_subdir() {
		if (properties == null) {
			init();
		}
		return pascalvoc_annotation_subdir;
	}

	public static String getPascalvoc_jpgimages_subdir() {
		if (properties == null) {
			init();
		}
		return pascalvoc_jpgimages_subdir;
	}

	public static String getPascalvoc_serialized_v_uri_jobj_filename() {
		if (properties == null) {
			init();
		}
		return pascalvoc_serialized_v_uri_jobj_filename;
	}

	public static String getPascalvoc_serialized_v_object_jobj_filename() {
		if (properties == null) {
			init();
		}
		return pascalvoc_serialized_v_object_jobj_filename;
	}

	public static String getHtml_path_about() {
		if (properties == null) {
			init();
		}
		return html_path_about;
	}

	public static String getCss_path_main() {
		if (properties == null) {
			init();
		}
		return css_path_main;
	}

	public static String getDocking_windows_theme() {
		if (properties == null) {
			init();
		}
		return docking_windows_theme;
	}

	public static String getIcon_path_datasetexplorer() {
		if (properties == null) {
			init();
		}
		return icon_path_datasetexplorer;
	}

}
